/**
 * Created by admin on 2017/4/6.
 */
function deleteThis(id,url){
    var isTrue = confirm("确定要删除？");
    if(isTrue == true){
        $.ajax({
            type:"POST",
            url:url+"?",
            data:{"admin_id" : id},
            dataType:'json',
            success:function(data){
                if(data.status == 200){
                    alert("成功！");
                    window.location.reload();
                }else{
                    alert(data.info);
                    return false;
                }
            },
            error: function () {
                alert("服务端出错！");
                return false;
            }
        });
    }
}

function tuiSong(id,url){
    var isTrue = confirm("确定要推送？");
    if(isTrue == true){
        $.ajax({
            type:"POST",
            url:url,
            data:{"id" : id},
            dataType:'json',
            success:function(data){
                if(data.status == 200){
                    alert("成功！");
                    window.location.reload();
                }else{
                    alert(data.info);
                    return false;
                }
            },
            error: function () {
                alert("服务端出错！");
                return false;
            }
        });
    }
}

function getByIdd(id,url){
    window.location.href=""

       /* $.ajax({
            type:"POST",
            url:url+"?",
            data:{"address_id" : id},
           dataType:'json',
            success:function(msg){
                if(msg.status == 200){

                    alert(msg);
                    window.location.reload();
                }else{
                    alert(msg.info);
                    return false;
                }
            },
            error: function () {
                alert("服务端出错！");
               // window.location=localhost/getInsuranceByAddress_id?address_id=id
                return false;
            }
        });
*/
}



function deleteServiceProject(id,idtwo,url){
    var isTrue = confirm("确定要删除？");
    if(isTrue == true){
        $.ajax({
            type:"POST",
            url:url+"?",
            data:{"order_id" : id,"service_id":idtwo},
            dataType:'json',
            success:function(data){
                if(data.status == 200){
                    alert("成功！");
                    window.location.reload();
                }else{
                    alert(data.info);
                    return false;
                }
            },
            error: function () {
                alert("服务端出错！");
                return false;
            }
        });
    }
}

/**
 * 编辑管理员
 * @param id                id
 * @param name              姓名
 * @param phone             电话
 * @param login_name        登录名
 * @param login_password    登录密码
 */
function updateThis(id,name,phone,login_name,login_password,info_type){
    document.getElementById("info_id").value = id;
    document.getElementById("info_name").value = name;
    document.getElementById("info_phone").value = phone;
    document.getElementById("info_login_name").value = login_name;
    document.getElementById("info_login_password").value = login_password;
    document.getElementById("info_type")[info_type].selected=true;
    $("#editAdminInfo").modal({
        "backdrop":"static",
        "keyboard":"true"
    });
}

/**
 * 编辑工程师信息
 * @param id                id
 * @param name              姓名
 * @param phone             电话
 * @param login_name        地址
 */
function updateEngineerPhone(id,name,phone,login_name){
    document.getElementById("phone_id").value = id;
    document.getElementById("phone_name").value = name;
    document.getElementById("phone_phone").value = phone;
    document.getElementById("phone_address").value = login_name;
    $("#editEngineerPhone").modal({
        "backdrop":"static",
        "keyboard":"true"
    });
}

/**
 * 修改
 * @param id
 * @param name
 * @param price
 * @param back
 */
function updateMaintain(id,nameone,name,price,back,service_kind){
    document.getElementById("project_id").value = id;
    document.getElementById("project_content").value = name;
    document.getElementById("project_price").value = price;
    document.getElementById("project_back").value = back;
    document.getElementById("project_name").value = nameone;
    document.getElementById("service_kind")[service_kind].selected=true;
    $("#editMaintain").modal({
        "backdrop":"static",
        "keyboard":"true"
    });
}


function updateNewsManage(id,title,content){
    document.getElementById("id").value = id;
    document.getElementById("title").value = name;
    document.getElementById("content").value = price;
    $("#editMaintain").modal({
        "backdrop":"static",
        "keyboard":"true"
    });
}

/**
 * 修改工人信息
 * @param id
 * @param name
 * @param phone
 * @param address
 * @param one
 * @param two
 * @param three
 * @param four
 * @param five
 * @param six
 */
function updateEmployee(id,name,phone,address,one,two,three,four,five,six){
    document.getElementById("employee_id").value = id;
    document.getElementById("employee_name").value = name;
    document.getElementById("employee_phone").value = phone;
    document.getElementById("employee_address").value = address;

    document.getElementById("one_type")[one].selected=true;
    document.getElementById("two_type")[two].selected=true;
    document.getElementById("three_type")[three].selected=true;
    document.getElementById("four_type")[four].selected=true;
    document.getElementById("five_type")[five].selected=true;
    document.getElementById("six_type")[six].selected=true;
    $("#editEmployeeInfo").modal({
        "backdrop":"static",
        "keyboard":"true"
    });
}


/**
 * select标签动态改变option内容
 * @param url
 */
function changeLableMenu(order_id,url) {
    var id = $("#service_class_key").val();
    var service_kind = $("#service_kind_key").val();
    if (order_id == 0){
        window.location.href = url + '?service_class=' + id + "&service_kind=" + service_kind + "&service_brand=0&service_standard=0&service_assembly=0&page=1";
    }else {
        window.location.href = url + '?service_class=' + id + "&service_kind=" + service_kind + "&order_id=" + order_id + "&service_brand=0&service_standard=0&service_assembly=0&page=1";
    }
}


/**
 * 详情
 * @param id
 * @param url
 * @param idname
 */
function thisDeltail(id,url,idname) {
    window.location.href = url+'?'+idname+'='+id;
}


/**
 * 详情
 * @param id
 * @param url
 */
function detailThis(id,url) {
    window.location.href = url+id;
}


function startOrderDetail(suggest_id,order_id) {
    $.ajax({
        type:"POST",
        url:"updateSuggestStatus?",
        data:{"suggest_id" : suggest_id},
        dataType:'json',
        success:function(data){
            if(data.status == 200){
                window.location.href = 'selectOrderDetailById?order_id='+order_id;
            }else{
                alert(data.info);
                return false;
            }
        },
        error: function () {
            alert("服务端出错！");
            return false;
        }
    });

}


/**
 * 改变select
 * @param id
 * @param class_one
 * @param class_two
 * @param lable_type
 * @param name
 */
function updateLable(id,class_one,class_two,lable_type,name) {
    document.getElementById("update_class_one")[class_one].selected=true;
    document.getElementById("update_class_two")[class_two].selected=true;
    document.getElementById("update_lable_type_1")[lable_type].selected=true;
    document.getElementById("update_lable_name").value = name;
    document.getElementById("update_lable_id").value = id;
    $("#editLable").modal({
        "backdrop":"static",
        "keyboard":"true"
    });
}

/**
 * 订单添加维修产品
 * @param order_id
 * @param service_id
 */
function orderAddServiceProject(order_id,service_id) {
    $.ajax({
        type:"POST",
        url:"orderAddServiceProjectAAA?",
        data:{"order_id" : order_id,"service_id":service_id},
        dataType:'json',
        success:function(data){
            if(data.status == 200){
                alert("成功！");
                window.location.reload();
            }else{
                alert(data.info);
                return false;
            }
        },
        error: function () {
            alert("服务端出错！");
            return false;
        }
    });
}


function dongJie(id,url) {
    $.ajax({
        type: "POST",
        url: url + "?",
        data: {"id": id},
        dataType: 'json',
        success: function (data) {
            if (data.status == 200) {
                alert("成功！");
                window.location.reload();
            } else {
                alert(data.info);
                return false;
            }
        },
        error: function () {
            alert("服务端出错！");
            return false;
        }
    });
}

function updateNewNumber(number,newId) {
    $.ajax({
        type: "POST",
        url: "updateNewNumber?",
        data: {"new_id": newId,"number":number},
        dataType: 'json',
        success: function (data) {
            if (data.status == 200) {
                alert("成功！");
                window.location.reload();
            } else {
                alert(data.info);
                return false;
            }
        },
        error: function () {
            alert("服务端出错！");
            return false;
        }
    });
}
function alertIdeaContent(idea_content) {
    document.getElementById("xiuGaiYiJianXiangQing").value =idea_content;
    $("#xiugaiyijian").modal({
        "backdrop":"static",
        "keyboard":"true"
    });
}

var isAutoClose = 1; //1自动2手动
var websocket = null;
var websocket_con = 0;

//debugger;
$(document).ready(function(){
    //判断当前浏览器是否支持WebSocket

    if ('WebSocket' in window) {
        initWebsocket();
    }
    else {
        alert('当前浏览器 Not support websocket!')
    }

    //连接成功建立的回调方法
    websocket.onopen = function () {
        //alert("websocket连接成功！");
        //setMessageInnerHTML("WebSocket连接成功");
    }

    //连接发生错误的回调方法
    websocket.onerror = function () {
        //alert("websocket连接失败，您可能无法实时接收消息！");
        //setMessageInnerHTML("WebSocket连接发生错误");
    };

    //接收到消息的回调方法
    websocket.onmessage = function (event) {
        websocket_con++;
        if(websocket_con <= 1){

            var confirm_info = confirm("消息内容："+event.data+"。确定去首页查看？");
            //debugger;
            if(confirm_info == true){
                //alert(event.data);
                //debugger;
                window.location.href = "selectSuggestList";
            }else{
                //websocket_con = 0;
                return false;
            }
        }
        //setMessageInnerHTML(event.data);
    }

    //连接关闭的回调方法
    websocket.onclose = function () {
        if(isAutoClose==1){
            initWebsocket();
        }
        //alert("您将要关闭实时提醒！")
        //setMessageInnerHTML("WebSocket连接关闭");
        isAutoClose=1;
    }

    //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function () {
        closeWebSocket();
    }

});
//websocket地址
function initWebsocket() {
    // var address = WEBSOCKET_URL;
    var address = "ws://121.41.47.67:8080/backmanage/websocket";
    //var address = "ws://139/jyguanjia/websocket/"+shop_id;
    // "ws://43.254.53.215:80/jyguanjia/websocket/"+shop_id
    websocket = new WebSocket(address);
}
//关闭WebSocket连接
function closeWebSocket() {
    isAutoClose=2;
    websocket.close();
}